﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

namespace KinectServer.Util
{
    /// <summary>
    /// 생산자-소비자 쓰레드간의 데이터 전달을 위한 버퍼 클래스
    /// </summary>
    /// <typeparam name="T">전달할 데이터</typeparam>
    public class SyncBuffer<T>
    {
        private readonly Object locker = new Object();
        private Queue<T> queue = new Queue<T>();

        public void Enqueue(T data)
        {
            lock (locker)
            {
                queue.Enqueue(data);

                System.Diagnostics.Debug.WriteLine("Enqueue - Buffer Size:" + queue.Count);

                Monitor.Pulse(locker);
            }
        }

        public T Dequeue()
        {
            lock (locker)
            {
                while (queue.Count == 0)
                {
                    Monitor.Wait(locker);
                }

                System.Diagnostics.Debug.WriteLine("Dequeue - Buffer Size:" + queue.Count);

                return queue.Dequeue();
            }
        }

    }
}
